package com.split;

import com.comparator.CompareAddress;
import com.entity.Commodity;
import com.entity.Order;
import com.entity.Size;
import com.entity.WareHouse;
import java.util.ArrayList;
import java.util.Collections;

public class GreedyStrategy implements Strategy{

    private static int subId=0;

    public ArrayList<Order> splitWareHouse(Order order,ArrayList<WareHouse> WareHouses){
        ArrayList<Order> newOrderForm=new ArrayList<>();
        CompareAddress compareAddress=new CompareAddress(order.getAddress());
        ArrayList<WareHouse> sortedWareHouses= WareHouses;
        Collections.sort(sortedWareHouses,compareAddress);
        ArrayList<Commodity> surplusGoods = order.getProductDetails();

        for (WareHouse wareHouse:sortedWareHouses) {
            ArrayList<Commodity> currentWareHouseShipment=new ArrayList<>();
            ArrayList<Commodity> remainingProducts=new ArrayList<>();
            for(Commodity commodity:surplusGoods){
                if(surplusGoods.contains(commodity))
                    currentWareHouseShipment.add(commodity);
                else
                    remainingProducts.add(commodity);
            }
            if(!currentWareHouseShipment.isEmpty()){
                surplusGoods=remainingProducts;
                subId++;
                Order newOrder=null;
                try{
                    newOrder=order.clone();
                }catch (Exception e){
                    e.printStackTrace();
                }
                newOrder.setOrderId(order.getOrderId()+"-"+subId);
                newOrderForm.add(newOrder);
            }
        }
        return newOrderForm;
    }

    private ArrayList<Order> splitPackages(Order order){
        ArrayList<Order> newOrderForm=new ArrayList<>();
        ArrayList<Commodity> currentPackages=new ArrayList<>();
        for(Commodity commodity:order.getProductDetails()){
            currentPackages.add(commodity);
            float total_weight=0;
            Size totalSize=new Size();
            for(Commodity c:currentPackages){
                total_weight+=c.getWeight();
                totalSize.Add(totalSize);
            }
            if(total_weight>20||totalSize.getLength()>1.5||totalSize.getWidth()>1.5){
                currentPackages.remove(0);
                Order newOrder=null;
                try{
                    newOrder=order.clone();
                }catch (Exception e){
                    e.printStackTrace();
                }
                newOrder.setProductDetails(currentPackages);
                newOrderForm.add(newOrder);
            }
            if(!currentPackages.isEmpty()){
                Order newOrder=null;
                try{
                    newOrder=order.clone();
                }catch (Exception e){
                    e.printStackTrace();
                }
                newOrder.setProductDetails(currentPackages);
                newOrderForm.add(newOrder);
            }
        }
        return newOrderForm;
    }

    @Override
    public ArrayList<Order> splitAnOrder(Order order,ArrayList<WareHouse> WareHouses){
        ArrayList<Order> newOrderForm=new ArrayList<>();
        for(Order newOrders:splitWareHouse(order,WareHouses)){
            newOrderForm.addAll(splitPackages(newOrders));
        }
        return newOrderForm;
    }
}
